<template>
  <div class="main-content">
    <!-- 列表页 -->
    <div v-if="showFlag">
      <el-form
          :inline="true"
          :model="searchForm"
          class="form-content"
      >
        <el-row :gutter="20" class="slt">
          <el-form-item label="昵称">
            <el-input prefix-icon="el-icon-search" v-model="searchForm.nicheng" placeholder="昵称" clearable></el-input>
          </el-form-item>
          <el-form-item class="select" label="性别" prop="xingbie">
            <el-select @change="xingbieChange" clearable v-model="searchForm.xingbie" placeholder="请选择性别">
              <el-option
                  v-for="(item,index) in xingbieOptions"
                  v-bind:key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="success" @click="search()">查询</el-button>
          </el-form-item>
        </el-row>

        <el-row class="ad">
          <el-form-item>
            <div class="operation-buttons top-operation-buttons">
              <el-button
                  type="success"
                  icon="el-icon-plus"
                  @click="addOrUpdateHandler()"
              >新增
              </el-button>
              <el-button
                  :disabled="dataListSelections.length <= 0"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteHandler()"
              >删除
              </el-button>
            </div>
          </el-form-item>
        </el-row>
      </el-form>
      <div class="table-content">
        <el-table class="tables" :size="'medium'" :show-header="true"
                  :border="true"
                  :fit="true"
                  :stripe="true"
                  :style="{width: '100%',fontSize:'14px',color:'#606266'}"
                  v-if="true"
                  :data="dataList"
                  v-loading="dataListLoading"
                  @selection-change="selectionChangeHandler">
          <el-table-column
              type="selection"
              align="center"
              width="50">
          </el-table-column>
          <el-table-column label="索引" align="center" type="index" width="50"/>
          <el-table-column
              align="center"
              prop="zhanghao"
              header-align="center"
              label="账号">
            <template slot-scope="scope">
              {{ scope.row.zhanghao }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="nicheng"
              header-align="center"
              label="昵称">
            <template slot-scope="scope">
              {{ scope.row.nicheng }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="xingbie"
              header-align="center"
              label="性别">
            <template slot-scope="scope">
              {{ scope.row.xingbie }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="shouji"
              header-align="center"
              label="手机">
            <template slot-scope="scope">
              {{ scope.row.shouji }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="youxiang"
              header-align="center"
              label="邮箱">
            <template slot-scope="scope">
              {{ scope.row.youxiang }}
            </template>
          </el-table-column>
          <el-table-column width="300" align="center"
                           header-align="center"
                           label="操作">
            <template slot-scope="scope">
              <div class="operation-buttons">
                <el-button type="success" icon="el-icon-tickets" size="mini"
                           @click="addOrUpdateHandler(scope.row.id,'info')">详情
                </el-button>
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="addOrUpdateHandler(scope.row.id)">
                  修改
                </el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteHandler(scope.row.id)">删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            clsss="pages"
            :layout="layouts"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            :total="totalPage"
            class="pagination-content"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>
  </div>
</template>
<script>
import axios from 'axios'
import AddOrUpdate from "./add-or-update";

export default {
  data() {
    return {
      searchForm: {
        key: ""
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag: false,
      layouts: 'total, sizes, prev, pager, next, jumper',
      xingbieOptions: ["男", "女"]
    };
  },
  created() {
    this.init();
    this.getDataList();
  },
  mounted() {

  },
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g, '');
    }
  },
  components: {
    AddOrUpdate,
  },
  methods: {

    init() {
      this.xingbieOptions = ["男", "女"]
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },

    xingbieChange(val) {
      this.searchForm.xingbie = val
      this.searchForm.mima = ''
    },

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
      }
      if (this.searchForm.nicheng !== '' && this.searchForm.nicheng !== undefined) {
        params['nicheng'] = '%' + this.searchForm.nicheng + '%'
      }
      if (this.searchForm.xingbie !== '' && this.searchForm.xingbie !== undefined) {
        params['xingbie'] = this.searchForm.xingbie
      }
      this.$http({
        url: "yonghu/page",
        method: "get",
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type != 'info') {
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type);
      });
    },
    // 查看评论
    // 下载
    download(file) {
      window.open(`${file}`)
    },
    // 删除
    deleteHandler(id) {
      var ids = id
          ? [Number(id)]
          : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "yonghu/delete",
          method: "post",
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.main-content {
  background-color: #f5f7fa;
  padding: 20px;
  min-height: 100%;

  .slt {
    margin: 0 0 20px 0;
    display: flex;
    justify-content: flex-start;

    .el-form-item {
      margin-right: 15px;
      margin-bottom: 0;
    }
  }

  .ad {
    margin-bottom: 20px;
  }

  .el-button {
    margin-right: 8px;
    height: 36px;
    line-height: 36px;
    padding: 0 16px;
    font-size: 14px;
    min-width: 80px;
  }

  .el-input {
    width: 180px;

    .el-input__inner {
      height: 36px;
      line-height: 36px;
    }
  }

  .el-select {
    width: 180px;

    .el-input__inner {
      height: 36px;
      line-height: 36px;
    }
  }

  .el-form-item {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
  }

  .el-form-item__content {
    display: flex;
    align-items: center;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;

    .el-button {
      padding: 6px 10px;
      font-size: 12px;
      margin: 3px;
      min-width: 70px;
      border-radius: 3px;

      &.el-button--success {
        background-color: #4a90e2;
        border-color: #4a90e2;
      }

      &.el-button--primary {
        background-color: #4a90e2;
        border-color: #4a90e2;
      }

      &.el-button--danger {
        background-color: #ff3b30;
        border-color: #ff3b30;
      }

      i {
        margin-right: 3px;
      }
    }

    .el-table-column--operation {
      .cell {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
      }
    }
  }

  .pagination-content {
    display: flex;
    justify-content: center;
    margin-top: 20px;
  }
}

.table-content {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  background-color: #ffffff;
  padding: 15px;

  .el-table {
    margin-bottom: 15px;
    border-radius: 5px;
    overflow: hidden;

    th {
      background-color: #f5f7fa;
      color: #606266;
      font-weight: 500;
      padding: 12px 0;
    }

    td {
      padding: 10px 0;
    }
  }
}

.tables {
  width: 100%;
}

.tables .el-table__header th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
  border-color: #ebeef5;
}

.tables .el-table__row {
  background-color: #ffffff !important;
}

.tables .el-table__row:hover {
  background-color: #f5f7fa !important;
}

.tables .el-table__row td {
  padding: 10px 0;
  color: #606266 !important;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.form-content {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.form-content .slt {
  background-color: transparent;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  width: 100%;
  margin-bottom: 10px;

  .el-form-item {
    margin-right: 20px;
    margin-bottom: 10px;
  }
}

.form-content .ad {
  margin-bottom: 15px;

  .el-button {
    height: 40px;
    line-height: 40px;
    min-width: 100px;
    font-size: 14px;
    padding: 0 20px;
  }
}

.form-content .slt .el-input__inner {
  height: 40px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  background-color: #fff;
  color: #606266;
  font-size: 14px;
  padding: 0 12px;
  transition: all 0.3s;
}

.form-content .slt .el-input__inner:focus {
  border-color: #409eff;
  outline: none;
}

.form-content .slt .el-select .el-input__inner {
  padding-right: 30px;
}

.form-content .slt .el-form-item {
  margin-right: 20px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}

.form-content .slt .el-form-item__content {
  display: flex;
  align-items: center;
}

.form-content .slt .el-form-item__label {
  color: #606266;
  font-size: 14px;
  font-weight: 500;
  line-height: 40px;
}

.form-content .slt .el-button--success {
  height: 40px;
  padding: 0 20px;
  font-size: 14px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  border-radius: 4px;
  font-weight: 500;
  transition: all 0.3s;
  display: flex;
  align-items: center;
}

.form-content .slt .el-button--success:hover {
  background-color: #85ce61;
  border-color: #85ce61;
  color: #fff;
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

/* 操作按钮样式优化 */
.operation-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;

  .el-button {
    margin: 3px;
    height: 32px;
    line-height: 32px;
    padding: 0 10px;
    min-width: 70px;
  }
}

.top-operation-buttons {
  justify-content: flex-start;
}

.top-operation-buttons .el-button {
  margin-right: 10px;
  font-size: 14px;
  height: 40px;
  min-width: 100px;
  border-radius: 4px;
  font-weight: 500;
  padding: 0 16px;
  transition: all 0.3s;
}

.top-operation-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.top-operation-buttons .el-button--success {
  background-color: #67c23a;
  border-color: #67c23a;
}

.top-operation-buttons .el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

.operation-buttons .el-button {
  margin: 3px;
  flex-grow: 0;
  min-width: 74px;
  font-size: 12px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.25s ease;
}

.operation-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.operation-buttons .el-button--success {
  background-color: #67c23a;
  border-color: #67c23a;
}

.operation-buttons .el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.operation-buttons .el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

/* 覆盖默认的表格背景色 */
/deep/ .el-table,
/deep/ .el-table tr,
/deep/ .el-table__body {
  background-color: #ffffff !important;
}

/deep/ .el-table th {
  background-color: #f5f7fa !important;
}

/deep/ .el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 修改表头和整体表格样式 */
/deep/ .el-table th.is-leaf {
  border-bottom: 1px solid #ebeef5;
}

/deep/ .el-table__fixed-right::before,
/deep/ .el-table__fixed::before {
  background-color: #ebeef5;
}

.form-content .ad .operation-buttons .el-button {
  height: 40px !important;
  line-height: 40px;
  padding: 0 20px;
  font-size: 14px;
  min-width: 100px;
}

.form-content .slt .el-select {
  width: 180px;
}

.form-content .slt .el-input {
  width: 180px;
}

.form-content .slt .el-button {
  min-width: 100px;
}
</style>
